package com.tracker

import java.util.Date;
import java.util.List;
import com.commonProject.common.exception.ValidateException
import java.text.SimpleDateFormat;

import com.commonProject.common.classes.RequestDTO;
import com.commonProject.common.intefaces.IRequest



class RequestService {
	static transactional = true
	IRequest requestServiceProxy
	
		def getlist(params) {
			println("RequestService GETLIST")		
			
			Long request_id=Long.valueOf(params.get("id"))
			List<RequestDTO> el=null
			el=requestServiceProxy.getlist(request_id)
			return el.collect{[
				text : it.title,
				id   : it.id,
				leaf : "true"
			]}
		}
		
	def getelement(params){
		println("RequestService GETELEMENT")
		println(params)
		Long request_id=Long.valueOf(params.get("id"))
		println("request_id ="+request_id)
		RequestDTO el=null		
		println("id"+request_id)
		 el=requestServiceProxy.getelement(request_id)
		 
		return[
			'success' : 'true',
			id : el.id,
			title : el.title,
			company_name : el.company_name,
			count_persons : el.count_persons,
			name_tourist : el.name_tourist,
			//photo : el.photo,
			phone_number : el.phone_number,
			description : el.description,
			birthDate : el.birthDate
		]		
	}
	
	
	def deleteElement(params){
		println("RequestService DELETE ")
		println(params)
		Long request_id=Long.valueOf(params.get("id"))
		
		RequestDTO el=null
		try{
			el=requestServiceProxy.deleteElement(request_id)
		}catch(Exception ex){
			//println "-->EXCEPTION: ${ex}"
			println "No server connection"
			return [success:false]
		}
		println("*****")		
		return [success:true]
	}
	
	def editElement(params){
		println("Request EDIT")
		
		Long r_id=Long.valueOf(params.get("id"))
		String r_title=params.get('title')
		String r_company_name=params.get('companyName')
		Integer r_countPersons=Integer.parseInt(params.get('countPersons'))
		String r_touristName=params.get('touristName')
		Integer r_phoneNumber = Integer.parseInt(params.get('phoneNumber'))
		String r_description = params.get('description')
		String r_birthDate=params.get('birthDate')
		try{
			def el=requestServiceProxy.editElement(r_id,r_title,r_company_name,r_countPersons,r_touristName,r_phoneNumber,r_description,r_birthDate)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}
	
	/*
	def editElement(params){
		println("Request EDIT")

		def edit = Request.get(params.get('id'))
		edit.title = params.get('title')
		edit.company_name=params.get('companyName')
		edit.count_persons=Integer.parseInt(params.get('countPersons'))
		edit.name_tourist=params.get('touristName')
		edit.phone_number=Integer.parseInt(params.get('phoneNumber'))		
		edit.description=params.get('description')
		
		if (params.get('birthDate')){
			edit.birthDate=new Date(params.get('birthDate'))
		}else{
			edit.birthDate = null
		}
		if(edit.save()){
			return['success':'true']
		}else{
			def err = edit.errors.allErrors.collect{[
				errorMessage:messageSource.getMessage(it, null)
			]}
			println(err)
			return [
				success :'false',
				errors :err				
				]
		}
	}*/
	
	      
	def addElement(params) {
		println("ADD Request Client")
		println(params)
				
		Long r_id=Long.valueOf(params.get('id'))
		String r_title=params.get('title')
		String r_company_name=params.get('CompanyName')
		Integer r_count_persons=Integer.parseInt(params.get('count_persons'))		
		String r_name_tourist=params.get('tourist_name')
		Long r_phone_number=Long.valueOf(params.get('PhoneNumber'))
		String r_description=params.get('description')	
		
		try{		
			def el=requestServiceProxy.addElement(r_id,r_title,r_company_name,r_count_persons,r_name_tourist,r_phone_number,r_description)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}
	
	
	/*def addElement(params) {
		println("ADD Request")
		println(params)
		def time=Times.get(params.get('id'))
		if (time) {
			def newRequest=new Request()
			newRequest.title=params.get('title')
			newRequest.company_name=params.get('CompanyName')			
			newRequest.count_persons=Integer.parseInt(params.get('count_persons'))			
			newRequest.name_tourist=params.get('tourist_name')
			newRequest.phone_number=Integer.parseInt(params.get('PhoneNumber'))
			newRequest.description=params.get('description')
			
			newRequest.birthDate = new Date(params.get('birthDate'))
			time.addToRequest(newRequest)
			if (newRequest.save()) {
				return ['success':'true']			
			}else {
					def err = newRequest.errors.allErrors.collect{[
					errorMessage:messageSource.getMessage(it, null)
				]}
				println(err)
				return [
					success :'false',
					errors :err				
					]	
			}
		}else {
			return ['success':'false']			
		}
	}*/	
}
			


